package com.wego.controller;
import com.wego.bean.StateBean;
import com.wego.bean.TableBean;
import com.wego.common.bean.PageBean;
import com.wego.entity.domain.Carousel;
import com.wego.entity.domain.Goods;
import com.wego.entity.query.CarouselQuery;
import com.wego.ex.GlobalException;
import com.wego.service.CarouselService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Controller
@RequestMapping("/manager/carousel")
public class CarouselController {

    @Autowired
    private CarouselService carouselService;

    @GetMapping("/list")
    public @ResponseBody TableBean list(CarouselQuery carouselQuery) {
        //构造动态查询条件
        PageBean<Carousel> carouselPageBean = carouselService.selectPage(carouselQuery);
        List<Carousel> carousel = carouselPageBean.getResult();
        Long count = carouselPageBean.getTotal();
        TableBean tableBean = new TableBean<Carousel>();
        tableBean.setCode(0);
        tableBean.setMsg("");
        tableBean.setData(carousel);
        tableBean.setCount(count);
        return tableBean;
    }

    @GetMapping("/openAddUpdate")
    public String openAddUpdate(Long id, Model model) {
        if (id != null) {
            Carousel carousel = carouselService.selectByPrimaryKey(id);
            model.addAttribute("carousel", carousel);
        }
        return "manager/carousel_add_update";
    }

    @PostMapping("/addUpdate")
    public @ResponseBody StateBean addUpdate(Carousel carousel) {
        carousel.setUpdateTime(LocalDateTime.now());
        boolean res;
        String msg;
        Long id = carousel.getId();
        if (id != null) {
            //修改
            res = carouselService.updateByPrimaryKeySelective(carousel);
            msg = res ? "修改成功！" : "修改失败";
        } else {
            //添加
            carousel.setCreateTime(LocalDateTime.now());
            res = carouselService.insertSelective(carousel);
            msg = res ? "添加成功！" : "添加失败";
        }
        return StateBean.builder()
                .code(200)
                .message("成功")
                .build();
    }

    @GetMapping("/updateState")
    public @ResponseBody StateBean updateState(Long id, Model model) {
        Carousel carousel = carouselService.selectByPrimaryKey(id);
        Integer state = carousel.getState();
        if (state == null && id == null) {
            throw new GlobalException(400, "执行修改状态时提交的参数不正确", "list");
        }
        //修改状态的值
        carousel.setState(state == 1 ? 0 : 1);
        carousel.setUpdateTime(LocalDateTime.now());
        carouselService.updateByPrimaryKeySelective(carousel);
        return new StateBean(200,"更新成功");
    }

    @GetMapping("/batchDelete")
    public @ResponseBody StateBean batchDelete(String ids) {
        boolean res = carouselService.batchDelete(ids);
        return StateBean.builder()
                .code(200)
                .message("成功")
                .build();
    }

    @PostMapping("/deleteById")
    public @ResponseBody StateBean DeleteById(int id) {
        boolean res = carouselService.deleteByPrimaryKey(id);
        return StateBean.builder()
                .code(200)
                .message("成功")
                .build();
    }
}
